﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace CRM.Common
{
   public class Configration
    {
        public static DateTime GetDuringTime
        {
            get
            {
                return DateTime.Parse(ConfigurationManager.AppSettings.Get("DuringTime")).ToUniversalTime();
            }
        }
        public static string[] UploadFileTypes
        {
            get { return ConfigurationManager.AppSettings.Get("UPLOADFILE_TYPE").ToString().Split("|".ToCharArray()); }
        }
        public static long UploadFileLength
        {
            get { return long.Parse(ConfigurationManager.AppSettings.Get("UPLOADFILE_LENGTH")) * 1024; }
        }

        #region read configuration
        // Protected Methods (6) 
        /// <summary>
        /// Gets the app setting.
        /// </summary>
        /// <param name="appSettingName">Name of the app setting.</param>
        /// <returns></returns>
        public static string GetAppSetting(string appSettingName)
        {
            if (ConfigurationManager.AppSettings[appSettingName] == null)
                throw new NullReferenceException(appSettingName +
                                                 " AppSettings value cannot be null or empty in the web.config.");
            return ConfigurationManager.AppSettings[appSettingName];
        }

        /// <summary>
        /// Gets the bool app setting.
        /// </summary>
        /// <param name="appSettingName">Name of the app setting.</param>
        /// <returns></returns>
        public static bool GetBoolAppSetting(string appSettingName)
        {
            return Convert.ToBoolean(GetAppSetting(appSettingName));
        }

        /// <summary>
        /// Gets the connection string.
        /// </summary>
        /// <param name="connectionStringName">Name of the connection string.</param>
        /// <returns></returns>
        public static string GetConnectionString(string connectionStringName)
        {
            if (ConfigurationManager.ConnectionStrings[connectionStringName] == null)
                throw new NullReferenceException(connectionStringName +
                                                 " ConnectionStrings value cannot be null in the web.config.");

            if (String.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString))
                throw new NullReferenceException(connectionStringName +
                                                 " ConnectionStrings value cannot be null or empty in the web.config.");

            return ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
        }

        /// <summary>
        /// Gets the date time app setting.
        /// </summary>
        /// <param name="appSettingName">Name of the app setting.</param>
        /// <returns></returns>
        public static DateTime GetDateTimeAppSetting(string appSettingName)
        {
            DateTime dt;
            if (!DateTime.TryParse(GetAppSetting(appSettingName), out dt))
                throw new InvalidCastException(appSettingName + " AppSettings value is an invalid DateTime.");
            return dt;
        }

        /// <summary>
        /// Gets the int app setting.
        /// </summary>
        /// <param name="appSettingName">Name of the app setting.</param>
        /// <returns></returns>
        public static int GetIntAppSetting(string appSettingName)
        {
            int i;
            if (!int.TryParse(GetAppSetting(appSettingName), out i))
                throw new InvalidCastException(appSettingName + " AppSettings value is an invalid integer.");
            return i;
        }

        /// <summary>
        /// Gets the string array setting.
        /// </summary>
        /// <param name="appSettingName">Name of the app setting.</param>
        /// <returns></returns>
        public static string[] GetStringArraySetting(string appSettingName)
        {
            string[] values = GetAppSetting(appSettingName).Split(new[] { ',' });
            return values;
        }
        #endregion Methods 
    }
}
